//
//  NoteListViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "NoteListViewController.h"
#import "AssetManager.h"
#import "EditNoteViewController.h"

#define CELL_LABEL_NAME  9901
#define CELL_LABEL_DATE  9902

@implementation NoteListViewController
@synthesize notes;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    self.notes = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    AssetManager *am = [[AssetManager alloc] init];
    self.notes = [am getAllNotes];
    [am release];
    NSLog(@"notes array contents: %@", [self.notes description]);
    
    /*
    if([self.notes containsObject:@".DS_Store"])
    {
        [self.notes r];
    }
     */
    
    [tblView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.notes){
        return [self.notes count];
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"note_cell_id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        cell.imageView.image = [UIImage imageNamed:@"note.png"];
        
        UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, 180, 34)];
        lblName.tag = CELL_LABEL_NAME;
        lblName.backgroundColor = [UIColor clearColor];
        lblName.textColor = [UIColor blackColor];
        [cell addSubview:lblName];
        [lblName release];
        
        UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(230, 5, 85, 34)];
        lblDate.tag = CELL_LABEL_DATE;
        lblDate.backgroundColor = [UIColor clearColor];
        lblDate.textAlignment = UITextAlignmentRight;
        lblDate.font = [UIFont boldSystemFontOfSize:12.0];
        lblDate.textColor = [UIColor redColor];
        [cell addSubview:lblDate];
        [lblDate release];
        
    }
    
    UILabel *lblName = (UILabel *)[cell viewWithTag:CELL_LABEL_NAME];
    UILabel *lblDate = (UILabel *)[cell viewWithTag:CELL_LABEL_DATE];
    
    NSString *noteName, *noteDate;
    NSArray *subStrings = [[notes objectAtIndex:indexPath.row] componentsSeparatedByString:@"___-"];
    noteName = [subStrings objectAtIndex:0];
    noteDate = [subStrings objectAtIndex:1];

    lblName.text = noteName;
    lblDate.text = noteDate;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    EditNoteViewController *envc = [[EditNoteViewController alloc] init];
    envc.create = NO;
    envc.name = [self.notes objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:envc animated:YES];
    [envc release];
}

@end
